package com.shuqi.activity.viewport;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.shuqi.controller.R;

public class EmailAutoCompleteTextView extends AutoCompleteTextView {
    private static final String TAG = "EmailAutoCompleteTextView";

    private String[] emailSufixs = new String[] {"@qq.com", "@163.com", "@126.com", "@sina.com",
            "@yahoo.com", "@gmail.com", "@sina.cn", "@hotmail.com", "@sohu.com", "@foxmail.com"};

    public EmailAutoCompleteTextView(Context context) {
        super(context);
        init(context);
    }

    public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EmailAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(final Context context) {
        this.setAdapter(new EmailAutoCompleteAdapter(context, R.layout.email_auto_complete_item,
                emailSufixs));
        this.setThreshold(1);
        this.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String text = EmailAutoCompleteTextView.this.getText().toString();
                    if (!"".equals(text))
                        performFiltering(text, 0);
                }
            }
        });
    }

    @Override
    protected void replaceText(CharSequence text) {
        String t = this.getText().toString();
        int index = t.indexOf("@");
        if (index != -1)
            t = t.substring(0, index);
        super.replaceText(t + text);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        String t = text.toString();
        int index = t.indexOf("@");
        if (index == -1) {
            if (t.matches("^[a-zA-Z0-9_]+$")) {
                super.performFiltering("@", keyCode);
            } else {
                this.dismissDropDown();
            }
        } else {
            String mailText = t.substring(index);
            boolean flag = false;
            for (String str : emailSufixs) {
                if (str.equals(mailText)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                this.dismissDropDown();
            } else {
                super.performFiltering(t.substring(index), keyCode);
            }
        }
    }

    private class EmailAutoCompleteAdapter extends ArrayAdapter<String> {

        public EmailAutoCompleteAdapter(Context context, int textViewResourceId, String[] email_s) {
            super(context, textViewResourceId, email_s);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                v =
                        LayoutInflater.from(getContext()).inflate(
                                R.layout.email_auto_complete_item, null);
            }
            TextView tv = (TextView) v.findViewById(R.id.tv);
            String t = EmailAutoCompleteTextView.this.getText().toString();
            int index = t.indexOf("@");
            if (index != -1) {
                t = t.substring(0, index);
            }
            tv.setText(t + getItem(position));
            return v;
        }
    }
}
